<script lang="ts" setup name="CategoryDialog">
import type { FormInstance } from 'element-plus'
import { stockDetailList } from '~/src/list/goods'
import { Emits, Params } from '.'
const params = reactive<Params>({
  visible: false,
  form: {
    typeId: 1,
    sort: null,
    isShow: false,
    name: '',
    parentId: null,
    id: 0,
  },
  title: '',
  optionsCategory: [],
})

const { visible, optionsCategory, title } = toRefs(params)

const putParams = async (data: Params) => {
  params.visible = data.visible
  params.optionsCategory = data.optionsCategory
  params.form = data.form
  params.title = data.title
}

defineExpose({
  putParams,
})

const rules = {
  // parentId: [{ required: true, message: '请选择分类', trigger: ['change'] }],
  name: [{ required: true, message: '请输入名字', trigger: ['change'] }],
}

const emit = defineEmits<Emits>()
const form = ref<FormInstance>()
const onConfirm = async () => {
  await form.value?.validate()
  params.visible = false
  emit('onConfirm', params)
}
</script>

<template>
  <vab-dialog v-model:modelValue="visible" :title="title + '商品分类'" :width="600">
    <el-form ref="form" label-suffix="：" label-width="130px" :model="params.form" :rules="rules">
      <el-form-item label="上级分类" prop="parentId">
        <el-tree-select
          v-model="params.form.parentId"
          check-strictly
          :data="optionsCategory"
          node-key="id"
          :props="{ label: 'name', value: 'id' }"
          :render-after-expand="false"
        />
      </el-form-item>
      <el-form-item label="商品分类名称" prop="name">
        <el-input v-model="params.form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="销售方式" prop="typeId">
        <el-radio-group v-model="params.form.typeId">
          <el-radio v-for="item in stockDetailList" :key="item.value" :label="item.label" :value="item.value" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input-number v-model="params.form.sort" :controls="false" :max="10" :min="1" />
      </el-form-item>
      <el-form-item label="前台销售不展示" prop="isShow">
        <el-switch v-model="params.form.isShow" active-color="var(--el-color-primary)" inactive-color="#ff4949" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="onConfirm">保存</el-button>
      <el-button @click="visible = false">取消</el-button>
    </template>
  </vab-dialog>
</template>

<style lang="scss" scoped></style>
